//
//  AMapUtil.m
//  OnlineRetailers
//
//  Created by mini2 on 2017/6/16.
//  Copyright © 2017年 OnlineRetailers. All rights reserved.
//

#import "AMapUtil.h"
#import <AMapFoundationKit/AMapFoundationKit.h>
#import <AMapLocationKit/AMapLocationKit.h>
#import <AMapSearchKit/AMapSearchKit.h>

@interface AMapUtil()<AMapSearchDelegate>

///定位服务
@property(nonatomic, strong)  AMapLocationManager *locationManager;
///搜索
@property(nonatomic, strong) AMapSearchAPI *search;

@end

@implementation AMapUtil

#pragma mark -- AMapSearchDelegate

- (void)onGeocodeSearchDone:(AMapGeocodeSearchRequest *)request response:(AMapGeocodeSearchResponse *)response
{
    if (response.geocodes.count == 0){
        if(self.onMapSearchBlock){
            self.onMapSearchBlock(@"", @"");
        }
        return;
    }
    
    //解析response获取地理信息，具体解析见 Demo
    AMapGeocode *geoCode = response.geocodes.firstObject;
    if(self.onMapSearchBlock){
        self.onMapSearchBlock([NSString stringWithFormat:@"%f", geoCode.location.longitude], [NSString stringWithFormat:@"%f", geoCode.location.latitude]);
    }
}

#pragma mark
#pragma mark -- private

///初始化高德地图
-(void)initAMap
{
    [AMapServices sharedServices].apiKey = AMAP_KEY;
}

///定位
-(void)opneOneLocationWithBlock:(LocationBlock)block
{
    // 带逆地理（返回坐标和地址信息）。将下面代码中的 YES 改成 NO ，则不会返回地址信息。
    [self.locationManager requestLocationWithReGeocode:YES completionBlock:^(CLLocation *location, AMapLocationReGeocode *regeocode, NSError *error) {
        if (error){
            NSLog(@"locError:{%ld - %@};", (long)error.code, error.localizedDescription);
            
            if (error.code == AMapLocationErrorLocateFailed){
                if(block){
                    block(@"", @"");
                }
                return;
            }
        }
        
        CLLocationCoordinate2D pt = location.coordinate;
        NSLog(@"location:%@", location);
        if(block){
            block([NSString stringWithFormat:@"%lf", pt.longitude], [NSString stringWithFormat:@"%lf", pt.latitude]);
        }
        
        if (regeocode){
            NSLog(@"reGeocode:%@", regeocode);
        }
    }];
}

///定位带地址信息
-(void)opneOneLocationAddressWithBlock:(LocationAddressBlock)block
{
    [self.locationManager requestLocationWithReGeocode:YES completionBlock:^(CLLocation *location, AMapLocationReGeocode *regeocode, NSError *error) {
        if (error){
            NSLog(@"locError:{%ld - %@};", (long)error.code, error.localizedDescription);
            
            if (error.code == AMapLocationErrorLocateFailed){
                if(block){
                    block(@"", @"",@"",@"");
                }
                return;
            }
        }
        
        CLLocationCoordinate2D pt = location.coordinate;
        NSLog(@"location:%@", location);
        if(block){
            NSString *lableAddress=@"";
            if(regeocode.POIName.length<=0){
                if(regeocode.street.length>0){
                    lableAddress=regeocode.street;
                }
                
            }else{
                lableAddress=regeocode.POIName;
            }
            block([NSString stringWithFormat:@"%lf", pt.longitude], [NSString stringWithFormat:@"%lf", pt.latitude],regeocode.city,[NSString stringWithFormat:@"%@%@",regeocode.city,regeocode.district]);
        }
            
    }];

}

///地址检索
-(void)mapSearchForAddress:(NSString*)addr andBlock:(MapSearchBlock)block
{
    self.onMapSearchBlock = block;
    AMapGeocodeSearchRequest *geo = [[AMapGeocodeSearchRequest alloc] init];
    geo.address = addr;
    [self.search AMapGeocodeSearch:geo];
}

#pragma mark
#pragma mark -- getter,setter

///定位服务
- (AMapLocationManager *)locationManager
{
    if(!_locationManager){
        _locationManager = [[AMapLocationManager alloc] init];
        // 带逆地理信息的一次定位（返回坐标和地址信息）
        [_locationManager setDesiredAccuracy:kCLLocationAccuracyHundredMeters];
        //   定位超时时间，最低2s，此处设置为2s
        _locationManager.locationTimeout =2;
        //   逆地理请求超时时间，最低2s，此处设置为2s
        _locationManager.reGeocodeTimeout = 2;
    }
    return _locationManager;
}

///搜索
- (AMapSearchAPI *)search
{
    if(!_search){
        _search = [[AMapSearchAPI alloc] init];
        _search.delegate = self;
    }
    return _search;
}

@end
